import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import topLevelAwait from 'vite-plugin-top-level-await'
import path from 'path'

/**
 * command : serve | build
 * mode : development | production
 */
export default defineConfig(({ command, mode }) => {
    console.log(mode, command, '================================')
    return {
        plugins: [
            vue(),
            // ElementPlus({ useSource: true,}),
            AutoImport({
                resolvers: [
                    ElementPlusResolver(),
                ]
            }),
            Components({
                resolvers: [
                    ElementPlusResolver({
                        sideEffect: true
                    }),
                ]
            }),
            topLevelAwait({
                promiseExportName: '__tla',
                promiseImportName: i => `__tla_${i}`
            })
        ],
        server: {
            open: true,
            https: false,
            port: 8888,
            proxy: {
                '/api': {
                    target: 'http://127.0.0.1:3000',
                    changeOrigin: true,
                    rewrite: (path) => path.replace(/\/api/, '')
                },
            }
        },
        build: {
            // 输出目录
            outDir: 'mgr',
            // 资源目录
            assetsDir: 'assets',
            // 是否生成source map
            sourcemap: false,
            // 使用 terser 进行压缩
            minify: 'terser',
            // 压缩配置
            terserOptions: {
                compress: {
                    drop_console: true, // 生产环境下删除console
                },
            },
            // 配置rollup
            rollupOptions: {
                output: {
                    // 将所有的未使用资源进行移除
                    manualChunks: undefined,
                    // 在这里修改静态资源路径
                    chunkFileNames: 'static/assets/js/[name]-[hash].js',
                    entryFileNames: 'static/assets/js/[name]-[hash].js',
                    assetFileNames: 'static/assets/[ext]/[name]-[hash].[ext]',
                }
            }
        },
        base: './', // 这里更改打包相对绝对路径
        minify: true, // 是否压缩代码
        publicDir: true,
        resolve: {
            // 配置别名
            alias: [
                { find: '@', replacement: path.resolve(__dirname, 'src/') },
                { find: 'hooks', replacement: path.resolve(__dirname, 'src/hooks/') },
                { find: 'components', replacement: path.resolve(__dirname, 'src/components/') },
                { find: 'plugins', replacement: path.resolve(__dirname, 'src/plugins/') },
                { find: 'views', replacement: path.resolve(__dirname, 'src/views/') },
                { find: 'layout', replacement: path.resolve(__dirname, 'src/layout/') },
                { find: 'utils', replacement: path.resolve(__dirname, 'src/utils/') },
                { find: 'api', replacement: path.resolve(__dirname, 'src/api/') },
                { find: 'config', replacement: path.resolve(__dirname, 'src/config/') }
            ],
            // 扩展文件后缀
            extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue']

        }
    }
})

